<template>
	<om-addpage style="padding-top: 35px;"  :menuList='menuList' :busCode="busCode" :showChooseBtn="ismenu" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey,
		getEditorTitle,
		hasEditor
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import moment from '@/static/js/moment.js'
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				ismenu:false,
				menuList:[],
				userIdCacheKey: 'qam-cgjy-incomingInspectionReport-userId-cache',
				orderInstIdCacheKey: 'qam-cgjy-incomingInspectionReport-orderInstId-cache',
				supplierIdCacheKey: 'qam-cgjy-incomingInspectionReport-supplierId-cache',
				reviewerIdCacheKey: 'qam-cgjy-incomingInspectionReport-reviewerId-cache',
				orderUserIdCacheKey: 'qam-cgjy-incomingInspectionReport-orderUserId-cache',
				unqualifiedNumber: -1,
				qualifiedInspectionNumber: '',
				supplierContactIdCacheKey: 'qam-cgjy-incomingInspectionReport-supplierContactId-cache',
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','salaryInstId','salaryInstName'])
		},
		methods:{
			onSubChange(editor){
				let _that = this.$refs.autoform
				let that = this 
				if(editor.groupKey == 'title3' && editor.workList.length) {
					_that.formdata.unqualifiedId = ""
					_that.formdata.unqualified = ""
					_that.formdata.keyId = ""
					_that.formdata.keyName = ""
					_that.formdata.importantId = ""
					_that.formdata.importantName = ""
					for(let i=0; i<editor.workList.length; i++){
						if(i==0){
							_that.formdata.unqualifiedId = editor.workList[i].batchUnqualifiedId
							_that.formdata.unqualified = editor.workList[i].batchUnqualified
						}else{
							if(_that.formdata.unqualifiedId>editor.workList[i].batchUnqualifiedId){
								_that.formdata.unqualifiedId = editor.workList[i].batchUnqualifiedId
								_that.formdata.unqualified = editor.workList[i].batchUnqualified
							}
						}
						
						if(editor.workList[i].keyFlagId){
							if(editor.workList[i].keyFlagId == 6212){
								_that.formdata.importantId = editor.workList[i].keyFlagId
								_that.formdata.importantName = "√"
							}else{
								_that.formdata.keyId = editor.workList[i].keyFlagId
								_that.formdata.keyName = "√"
							}
						}
						
						_that.formdata.disposalOfNonconformitiesId = editor.workList[i].disposalOfNonconformitiesId
						_that.formdata.disposalOfNonconformitiesName = editor.workList[i].disposalOfNonconformities
						_that.actualSample = editor.workList[i].actualSample
						editor.showAdd = false;
						let oldNewProductId = uni.getStorageSync("newProductIdKey");
						if(editor.workList[i].newProductId != oldNewProductId){
							that.getIncomingInspectionReport(editor.workList[i])
						}
						uni.setStorageSync('newProductIdKey', editor.workList[i].newProductId.toString())
					}
				}
				if(editor.groupKey == 'title5') {
					if(editor.workList.length>0){
						if(editor.workList[0].unqualifiedNumber){
							_that.unqualifiedNumber = editor.workList[0].unqualifiedNumber || -1
							uni.setStorageSync('unqualifiedNumberKey', _that.unqualifiedNumber.toString())
						}else{
							if(editor.workList[0].unqualifiedNumber == 0){
								uni.setStorageSync('unqualifiedNumberKey', '0')
							}else{
								uni.setStorageSync('unqualifiedNumberKey', '-1')
								uni.setStorageSync('qualifiedInspectionNumberKey', '-1')
							}
						}
						
						if(editor.workList[0].actualSample){
							uni.setStorageSync('actualSampleKey', editor.workList[0].actualSample.toString())
						}else{
							uni.setStorageSync('actualSampleKey', '-1')
						}
						
						if(editor.workList[0].rejectionNumber){
							uni.setStorageSync('rejectionNumberKey', editor.workList[0].rejectionNumber.toString())
						}else{
							uni.setStorageSync('rejectionNumberKey', '-1')
						}
					}else{
						uni.setStorageSync('unqualifiedNumberKey', '-1')
					}
				}
			},
			getIncomingInspectionReport(item){
				let _that = this
				_that.om.request(
					'incomingInspectionReport/findInspectionRecordDataList',
					{
						productId: item.newProductId,
					}
				).then(res=>{
					if(_that.subEditorObj["title4"]){
						let datalist = res.value
						_that.subEditorObj["title4"].workList = datalist
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"subDataStr",JSON.stringify(datalist))
						_that.$set(_that.formdata,"subDataStr",JSON.stringify(datalist))
					}
				})
				
				_that.om.request(
					'incomingInspectionReport/getRecordPlanList',
					{
						testRulesId: item.testRulesId,
						inspectionLevelId: item.inspectionLevelId,
						inspectionSchemeId: item.inspectionSchemeId,
						inspectionMethodId: item.inspectionMethodId,
						valueAQLId: item.valueAQLId
					}
				).then(res=>{
					if(_that.subEditorObj["title5"]){
						let datalist = res.value
						if(res.value.length>0){
							_that.unqualifiedNumber = res.value[0].unqualifiedNumber || -1
							uni.setStorageSync('unqualifiedNumberKey', _that.unqualifiedNumber.toString())
							_that.subEditorObj["title5"].workList = datalist
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataPlanStr",JSON.stringify(datalist))
							_that.$set(_that.formdata,"dataPlanStr",JSON.stringify(datalist))
						}else{
							uni.setStorageSync('unqualifiedNumberKey', '-1')
							_that.getDataPlan()
						}
					}
				})
			},
			getDataPlan(){
				let _that = this
				let datalist = [{
					sampleNumberId: '',
					sampleNumberName: '',
					actualSample: '',
					unqualifiedNumber: '',
					acceptanceNumber: '',
					rejectionNumber: '',
					actualSampleSum: '',
					unqualifiedNumberSum: ''
				}]
				if(datalist.length>0){
					_that.unqualifiedNumber = datalist[0].unqualifiedNumber || -1
					uni.setStorageSync('unqualifiedNumberKey', _that.unqualifiedNumber.toString())
				}else{
					uni.setStorageSync('unqualifiedNumberKey', '-1')
				}
				_that.subEditorObj["title5"].workList = datalist
				_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataPlanStr",JSON.stringify(datalist))
				_that.$set(_that.formdata,"dataPlanStr",JSON.stringify(datalist))
			},
			formSubmit(){
				let _that = this
				if(_that.subEditorObj["title3"]){
					let dataList = JSON.parse(_that.formdata.dataStr)
					 for(let index in dataList){
						 if(!dataList[index].fileList){
							 delete dataList[index].fileList
						 }
					 }
					 _that.formdata.dataStr = JSON.stringify(dataList)
				}
				if(_that.edit){
					_that.om.formSave("incomingInspectionReport/doUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("incomingInspectionReport/doAdd",_that.formdata,_that)
				}
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'289',
							categoryId:'8955'
						}
					},{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'283',
							categoryId:'6431'
						}
					},{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'289',
							categoryId:'7549'
						}
					},{
					url:'common/getMenu',
					param:{
						formCode:options.code
					},
				}]
					if(fileId){
						ajaxArr.push({
							url:"incomingInspectionReport/getValue",
							param:{
								fileId:fileId
							}
						})
					}

				formModelLoad(ajaxArr,function(res,mainData){
					// changeEditor("title1", "supplierContactId", {
					// 	"type": "editlinkbox",
					// 	"prompt": "请选择",
					// 	"linkParam":{
					// 		other: JSON.stringify({
					// 			tsupplierId: ''
					// 		}),
					// 		namefield:"userName",
					// 		initload:true,
					// 		url:"tsupplier/findSupplierContactList",
					// 		cachename:_that.supplierContactIdCacheKey
					// 	}
					// },null,mainData)
					// changeEditor("title1","supplierContactId","data",configSelectData([],"supplierStaffId","userName"),mainData)
					changeEditor("title2","reviewerPostId","data",configSelectData([],"id","posname"),mainData)
					changeEditor("title1","orderInstId","data",configSelectData([],'id','instName'),mainData)
					changeEditor("title1","disposalOfNonconformitiesId","data",configSelectData(res[0],'id','text'),mainData)
					changeEditor("title1","importantId","data",configSelectData(res[1],'id','text'),mainData)
					changeEditor("title1","unqualifiedId","data",configSelectData(res[2],'id','text'),mainData)
					_that.menuList =  res[3].value
					changeEditor("topinfo","zinstId",
						{
							defaultValue:_that.salaryInstId,
							defaultName:_that.salaryInstName
						}
					,null,mainData)
					
					changeEditor("title1","userId",{
						link:'../../public/searchUser/searchUser',
						"linkParam":{
							cachename: _that.userIdCacheKey,
							isSingle:true
						}
					},null,mainData)
					
					changeEditor("title1","orderUserId",{
						link:'../../public/searchUser/searchUser',
						"linkParam":{
							cachename: _that.orderUserIdCacheKey,
							isSingle:true
						}
					},null,mainData)
					
					changeEditor("title1","supplierId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								oldValue: ''
							}),
							url:'tsupplier/likeSerchAllSupplier',
							namefield:'supplierCode',
							notefield: "supplierName",
							initload:true,
							searchfield:'q',
							cachename:_that.supplierIdCacheKey
						}
					},null,mainData)
					
					changeEditor("title2","reviewerId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({}),
							url:'temployee/getStaffCombogrid',
							namefield:'userPosInstName',
							initload:true,
							searchfield:'q',
							cachename:_that.reviewerIdCacheKey
						}
					},null,mainData)
					
					if (_that.subEditorObj['title3']) {
						let configData = {}
						if(_that.subEditorObj["title3"]){
							changeEditor('title3', 'dataStr', {
								showDel:false,
								getWorkParam:'getWorkParam',
								titleTemplate: '存货编码：<%= newProductCode || "无" %>',
							}, null, mainData);
						}
					}
					
					if (_that.subEditorObj['title4']) {
						let configData = {}
						if(_that.subEditorObj["title4"]){						
							changeEditor('title4', 'subDataStr', {
								showDel:false,
								showAdd:false,
								titleTemplate: '检验项目：<%= projectName || "无" %>',
							}, null, mainData);
						}
					}
					
					if (_that.subEditorObj['title5']) {
						let configData = {}
						if(_that.subEditorObj["title5"]){						
							changeEditor('title5', 'dataPlanStr', {
								showDel:false,
								showAdd:false,
								titleTemplate: '样本不合格数：<%= unqualifiedNumber || "无" %>',
							}, null, mainData);
						}
					}
					_that.getDataPlan()
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[4].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[4].value[index])
								_that.$set(_that.formdata,index,res[4].value[index])
							}
						}
						changeEditor("title1","userId",{
							link:'../../public/pageSearch/pageSearch',
							"linkParam":{
								other:JSON.stringify({
									firstUserId: res[4].value.userId
								}),
								url:'temployee/getStaffCombogrid',
								namefield:'userPosInstName',
								initload:true,
								searchfield:'q',
								cachename:_that.userIdCacheKey
							}
						},null,mainData)
						
						changeEditor("title2","reviewerId",{
							link:'../../public/pageSearch/pageSearch',
							"linkParam":{
								other:JSON.stringify({
									firstUserId: res[4].value.reviewerId
								}),
								url:'temployee/getStaffCombogrid',
								namefield:'userPosInstName',
								initload:true,
								searchfield:'q',
								cachename:_that.reviewerIdCacheKey
							}
						},null,mainData)
						changeEditor("title1","supplierId",{
							link:'../../public/pageSearch/pageSearch',
							"linkParam":{
								other:JSON.stringify({
									oldValue: res[4].value.supplierId
								}),
								url:'tsupplier/likeSerchAllSupplier',
								namefield:'supplierCode',
								notefield: "supplierName",
								initload:true,
								searchfield:'q',
								cachename:_that.supplierIdCacheKey
							}
						},null,mainData)
						_that.om.request(
							'orgInstPosUserOfficial/getPosComboboxByUser',
							{
								userId: res[4].value.reviewerPostId,
								flag: true
							}
						).then(res=>{
							_that.$refs.autoform.changeEditor("title2","reviewerPostId","data",configSelectData(res,"id","posname"))
							_that.$refs.autoform.initSelectIndex("title2","reviewerPostId")
						})
						_that.om.request(
							'orgInstPosUserOfficial/findInstByUserIdOrPostId',
							{
								userId: res[4].value.orderUserId
							}
						,'post').then(res=>{
							_that.$refs.autoform.changeEditor("title1","orderInstId","data",configSelectData(res,'id','instName'))
							_that.$refs.autoform.initSelectIndex("title1","orderInstId")
						})
						_that.om.request(
							'orgInstPosUserOfficial/findInstByUserIdOrPostId',
							{
								userId: res[4].value.userId
							}
						,'post').then(res=>{
							_that.$refs.autoform.changeEditor("title1","instId","data",configSelectData(res,'id','instName'))
							_that.$refs.autoform.initSelectIndex("title1","instId")
						})						
						// _that.om.request(
						// 	'tsupplier/findSupplierContactList',
						// 	{
						// 		tsupplierId: res[4].value.supplierId
						// 	}).then(zres=>{
						// 		// _that.$refs.autoform.changeEditor("title1","supplierContactId","data",configSelectData(zres,"supplierStaffId","userName"))
						// 		// _that.$refs.autoform.initSelectIndex("title1","supplierContactId")
						// })
						
						if(_that.subEditorObj["title3"]){
							let datalist = res[4].value.incomingInspectionReportInspectionRecordList
							if(datalist.length>0){
								changeEditor("title3", "dataStr", {
									showAdd:false
								}, null, mainData)
							}
							_that.subEditorObj["title3"].workList = datalist
							uni.setStorageSync('newProductIdKey', (datalist[0].newProductId || -1).toString())
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(datalist))
							_that.$set(_that.formdata,"dataStr",JSON.stringify(datalist))
						}
						if(_that.subEditorObj["title4"]){
							let datalist = res[4].value.incomingInspectionReportInspectionRecordDataList
							_that.subEditorObj["title4"].workList = datalist
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"subDataStr",JSON.stringify(datalist))
							_that.$set(_that.formdata,"subDataStr",JSON.stringify(datalist))
						}
						if(_that.subEditorObj["title5"]){
							let datalist = res[4].value.incomingInspectionReportInspectionRecordPlanList
							if(datalist.length>0){
								_that.unqualifiedNumber = datalist[0].unqualifiedNumber || -1
								uni.setStorageSync('unqualifiedNumberKey', _that.unqualifiedNumber.toString())
								uni.setStorageSync('actualSampleKey', (datalist[0].actualSample || -1).toString())
								uni.setStorageSync('rejectionNumberKey', (datalist[0].rejectionNumber || -1).toString())
								_that.subEditorObj["title5"].workList = datalist
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataPlanStr",JSON.stringify(datalist))
								_that.$set(_that.formdata,"dataPlanStr",JSON.stringify(datalist))
							}else{
								uni.setStorageSync('unqualifiedNumberKey', '-1')
								uni.setStorageSync('actualSampleKey', '-1')
								uni.setStorageSync('rejectionNumberKey', '-1')
								_that.getDataPlan()
							}
						}
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("title1","disposalOfNonconformitiesId")
							_that.$refs.autoform.initSelectIndex("title1","keyId")
							_that.$refs.autoform.initSelectIndex("title1","importantId")
							_that.$refs.autoform.initSelectIndex("title1","unqualifiedId")
						})
					}
					
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.userId){
							_that.$refs.autoform.formdata.userId = _that.userId
							_that.$refs.autoform.formdata.userName = _that.userName
						}
						if(!_that.$refs.autoform.formdata.instId){
							_that.om.request(
								'orgInstPosUserOfficial/findInstByUserIdOrPostId',
								{
									userId: _that.$refs.autoform.formdata.userId
								}
							,'post').then(res=>{
								_that.$refs.autoform.formdata.instId = _that.salaryInstId
								_that.$refs.autoform.formdata.instName = _that.salaryInstName
								_that.$refs.autoform.changeEditor("title1","instId","data",configSelectData(res,'id','instName'))
								_that.$refs.autoform.initSelectIndex("title1","instId")
							})
						}
						if(!_that.$refs.autoform.formdata.incomingDate){
							_that.$refs.autoform.formdata.incomingDate = moment().format('YYYY-MM-DD')
						}
					})
				},options)
			},
			clearUserId(){
				let _that = this
				_that.$refs.autoform.clear("title1",'userId')
				_that.$refs.autoform.clear("title1",'instId')
				_that.$refs.autoform.changeEditor("title1","instId","data",configSelectData([],'id','instName'))
			},
			clearOrderUserId(){
				let _that = this
				_that.$refs.autoform.clear("title1",'orderInstId')
				_that.$refs.autoform.clear("title1",'orderUserId')
				_that.$refs.autoform.changeEditor("title1","orderInstId","data",configSelectData([],"id","instName"))
			},
			clearSupplierCoding(){
				let _that = this
				_that.$refs.autoform.clear("title1",'supplierId')
				_that.$refs.autoform.clear("title1",'supplierCode')
				_that.$refs.autoform.clear("title1",'supplierName')
				_that.$refs.autoform.clear("title1",'supplierShortName')
				_that.$refs.autoform.clear("title1",'supplierContactId')
				_that.$refs.autoform.clear("title1",'supplierContactNumber')
				_that.$refs.autoform.clear("title1",'unqualifiedBatchNumber')
				// _that.$refs.autoform.changeEditor("title1","supplierContactId","data",configSelectData([],"supplierStaffId","userName"))
			},
			onselectSupplierContact(item){
				let _that = this
				_that.$refs.autoform.formdata.supplierContactId = item.id
				_that.$refs.autoform.formdata.supplierContact = item.userName
				_that.$refs.autoform.formdata.supplierContactNumber = item.mobilePhone
			},
			clearSupplierContact(){
				let _that = this
				_that.$refs.autoform.clear("title1",'supplierContactId')
				_that.$refs.autoform.clear("title1",'supplierContactNumber')
			},
			clearReviewerId(){
				let _that = this
				_that.$refs.autoform.clear("title2",'reviewerId')
				_that.$refs.autoform.clear("title2",'reviewerPostId')
				_that.$refs.autoform.clear("title2",'reviewerInstId')
			},
			clearReviewerPostId(){
				let _that = this
				_that.$refs.autoform.clear("title2",'reviewerPostId')
				_that.$refs.autoform.clear("title2",'reviewerInstId')
			},
			onselectReviewerPostId(item){
				let _that = this
				_that.$refs.autoform.formdata.reviewerPostId = item.id
				_that.$refs.autoform.formdata.reviewerPostName = item.posname
				_that.$refs.autoform.formdata.reviewerInstId = item.instId
				_that.$refs.autoform.formdata.reviewerInstName = item.instName
			},
			getWorkParam(){
				let _that = this
				return {
					unqualifiedNumber:_that.unqualifiedNumber,
					qualifiedInspectionNumber:_that.qualifiedInspectionNumber,
				}
			},
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
			uni.setStorageSync('unqualifiedNumberKey', '')
			uni.setStorageSync('qualifiedInspectionNumberKey', '')
			uni.setStorageSync('newProductIdKey', '')
			uni.setStorageSync('actualSampleKey', '')
			uni.setStorageSync('rejectionNumberKey', '')
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (let fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							_that.onSubChange(editor)
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				};
				uni.getStorage({
					key:_that.userIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.userId = d.id
							formdata.userName = d.username
							_that.$refs.autoform.changeEditor('title1', 'userId', {
								linkParam: {
									other:JSON.stringify({
										firstUserId: d.id
									}),
									url:'temployee/getStaffCombogrid',
									namefield:'userPosInstName',
									initload:true,
									searchfield:'q',
									cachename:_that.userIdCacheKey
								}
							});
							_that.om.request(
								'temployee/getUserPosAddSalary',
								{
									userId: d.id
								}
							,'post').then(res=>{
								formdata.instId = res.value.salaryPosInstId
								formdata.instName = res.value.salaryPosInstName
								_that.om.request(
									'orgInstPosUserOfficial/findInstByUserIdOrPostId',
									{
										userId: d.id
									}
								,'post').then(zres=>{
									_that.$refs.autoform.changeEditor("title1","instId","data",configSelectData(zres,'id','instName'))
									_that.$refs.autoform.initSelectIndex("title1","instId")
								})
							})
							uni.removeStorage({
								key:_that.userIdCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.orderUserIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.orderUserId = d.id
							formdata.orderUserName = d.username
							_that.om.request(
								'temployee/getUserPosAddSalary',
								{
									userId: d.id
								}
							,'post').then(res=>{
								formdata.orderInstId = res.value.salaryPosInstId
								formdata.orderInstName = res.value.salaryPosInstName
								_that.om.request(
									'orgInstPosUserOfficial/findInstByUserIdOrPostId',
									{
										userId: d.id
									}
								,'post').then(zres=>{
									_that.$refs.autoform.changeEditor("title1","orderInstId","data",configSelectData(zres,'id','instName'))
									_that.$refs.autoform.initSelectIndex("title1","orderInstId")
								})
							})
							uni.removeStorage({
								key:_that.orderUserIdCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.reviewerIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							let isbhg = false;
							for(let i=0; i<_that.subEditorObj['title3'].workList.length; i++){
								if(_that.subEditorObj['title3'].workList[i].batchInspectionConclusionId == 6226){
									isbhg = true
								}
							}
							
							if(isbhg){
								errorModal(_that,'提示信息',"批检验结论没有不合格的物料，不能选择让步接收评审人！",()=>{
									_that.clearReviewerId()
								})
							}else if(d.id == formdata.userId){
								errorModal(_that,'提示信息',"让步接收评审人不能为检验员！",()=>{
									_that.clearReviewerId()
								})
							}else{
								formdata.reviewerId = d.id
								formdata.reviewerName = d.username
								_that.$refs.autoform.changeEditor('title2', 'reviewerId', {
									linkParam: {
										other:JSON.stringify({
											firstUserId: d.id
										}),
										url:'temployee/getStaffCombogrid',
										namefield:'userPosInstName',
										initload:true,
										searchfield:'q',
										cachename:_that.reviewerIdCacheKey
									}
								});
								_that.om.request(
									'orgInstPosUserOfficial/getPosComboboxByUser',
									{
										userId: d.id,
										flag: true
									}
								).then(res=>{
									formdata.reviewerPostId = d.salaryPosId
									formdata.reviewerPostName = d.salaryPosName
									formdata.reviewerInstId = d.salaryPosInstId
									formdata.reviewerInstName = d.salaryPosInstName
									_that.$refs.autoform.changeEditor("title2","reviewerPostId","data",configSelectData(res,"id","posname"))
									_that.$refs.autoform.initSelectIndex("title2","reviewerPostId")
								})
							}
							
							uni.removeStorage({
								key:_that.reviewerIdCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key: _that.orderInstIdCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							if(formdata.orderInstId != d.id){
								_that.om.request(
									'orgInstPosUserOfficial/findUserList',
									{
										instId: d.id
									},
								).then(res=>{
									_that.$refs.autoform.changeEditor("title1","orderUserId","data",configSelectData(res,"id","username"))
									_that.$refs.autoform.clear("title1",'orderUserId')
								})
							}
							
							formdata.orderInstId = d.id
							formdata.orderInstName = d.text	
							uni.removeStorage({
								key: _that.orderInstIdCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key:_that.supplierIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.supplierId = d.id
							formdata.supplierCoding = d.supplierCode
							formdata.supplierCode = d.supplierCode
							formdata.supplierName = d.supplierName
							formdata.supplierShortName = d.supplierShortName
							_that.$refs.autoform.changeEditor('title1', 'supplierId', {
								linkParam: {
									other:JSON.stringify({
										oldValue: d.id
									}),
									url:'tsupplier/likeSerchAllSupplier',
									namefield:'supplierCode',
									notefield: "supplierName",
									initload:true,
									searchfield:'q',
									cachename:_that.supplierIdCacheKey
								}
							});
							_that.om.request(
								'incomingInspectionReport/getSupplierProductQuality',
								{
									supplierId: d.id
								},
							'post').then(res=>{
								formdata.unqualifiedBatchNumber = res.value
							})
							_that.om.request(
								'tsupplier/getSupplierStaffModelDto',
								{
									tsupplierId: d.id
								},
							'post').then(res=>{
								formdata.supplierContactId = res.value.mainContactDto.id
								formdata.supplierContact = res.value.mainContactDto.userName
								formdata.supplierContactNumber = res.value.mainContactDto.mobilePhone
								
								// _that.$refs.autoform.changeEditor("title1", "supplierContact", {
								// 	"type": "editlinkbox",
								// 	"prompt": "请选择",
								// 	"linkParam":{
								// 		other: JSON.stringify({
								// 			tsupplierId: d.id
								// 		}),
								// 		namefield:"userName",
								// 		initload:true,
								// 		url:"tsupplier/findSupplierContactList",
								// 		cachename:_that.supplierContactIdCacheKey
								// 	}
								// })
								
								// _that.om.request(
								// 	'tsupplier/findSupplierContactList',
								// 	{
								// 		tsupplierId: d.id
								// 	}).then(zres=>{
								// 		_that.$refs.autoform.changeEditor("title1", "supplierContactId", {
								// 			"type": "editlinkbox",
								// 			"prompt": "请选择",
								// 			"linkParam":{
								// 				other: JSON.stringify({
								// 					eduCode:item.code
								// 				}),
								// 				namefield:"dicname",
								// 				initload:true,
								// 				searchByBackground:true,
								// 				searchfield:'q',
								// 				url:"major/getMajorList",
								// 				cachename:_that.studyDirectCacheKey
								// 			}
								// 		})
								// 		// _that.$refs.autoform.changeEditor("title1","supplierContactId","data",configSelectData(zres,"supplierStaffId","userName"))
								// 		// _that.$refs.autoform.initSelectIndex("title1","supplierContactId")
								// })
							})
							uni.removeStorage({
								key:_that.supplierIdCacheKey
							})
						}
					}
				});
				// uni.getStorage({
				// 	key: _that.supplierContactIdCacheKey,
				// 	success: function(res){
				// 		console.log(res)
				// 		if(res.data){
				// 			let d = res.data
				// 			var formdata  = _that.$refs.autoform.formdata
				// 			if(Object.prototype.toString.call(d) != "[object Object]"){
				// 				formdata.supplierContact = d
				// 			}else{
				// 				formdata.supplierContactId = d.id
				// 				formdata.supplierContact = d.userName
				// 			}
				// 			uni.removeStorage({
				// 				key: _that.supplierContactIdCacheKey
				// 			});
				// 		}
				// 	}
				// });
				try {
					const value = uni.getStorageSync('chooseData');
					
					if(value.length>1 || value[0].subData.length>1){
						errorModal(_that,'提示信息','只能选择一条产品，请重新选单！',()=>{
							
						})
					}else{
						if (value) {
							let arr = []
							let obj = {}
							let fieldArr = []
							if(_that.subEditorObj["title3"]){
								let conifgData = _that.subConfigData["title3"]
								fieldArr = getFormFields(conifgData)
								for(let index in fieldArr){
									_that.$set(obj,fieldArr[index],'')
								}
								_that.subEditorObj["title3"].showAdd = false
							}
							if(_that.subEditorObj["title4"]){
								let conifgData = _that.subConfigData["title4"]
								fieldArr = getFormFields(conifgData)
								for(let index in fieldArr){
									_that.$set(obj,fieldArr[index],'')
								}
							}
							let formdata = _that.$refs.autoform.formdata
							formdata.remark = value[0].remark
							formdata.orderUserId = value[0].purchasingSalesmanId
							formdata.orderUserName = value[0].purchasingSalesman
							formdata.orderInstId = value[0].departmentId
							formdata.orderInstName = value[0].department
							formdata.supplierId = value[0].supplierId
							formdata.supplierCoding = value[0].supplierCoding
							formdata.supplierCode = value[0].supplierCode
							formdata.supplierName = value[0].supplierName
							formdata.supplierContactId = value[0].supplierSalesmanId
							formdata.supplierContact = value[0].supplierSalesman
							formdata.supplierContactNumber = value[0].supplierContactNumber
							formdata.sourceFormId = value[0].busId
							formdata.sourceForm = value[0].busName
							formdata.sourceFormCode = value[0].code
							_that.om.request(
								'orgInstPosUserOfficial/findInstByUserIdOrPostId',
								{
									userId: value[0].purchasingSalesmanId
								}
							,'post').then(res=>{
								_that.$refs.autoform.changeEditor("title1","orderInstId","data",configSelectData(res,'id','instName'))
								_that.$refs.autoform.initSelectIndex("title1","orderInstId")
							})
							_that.om.request(
								'incomingInspectionReport/getSupplierProductQuality',
								{
									supplierId: value[0].supplierId
								},
							'post').then(res=>{
								formdata.unqualifiedBatchNumber = res.value
							})
							// _that.om.request(
							// 	'tsupplier/findSupplierContactList',
							// 	{
							// 		tsupplierId: value[0].supplierId
							// 	}).then(zres=>{
							// 		_that.$refs.autoform.changeEditor("title1","supplierContactId","data",configSelectData(zres,"id","userName"))
							// 		// _that.$refs.autoform.initSelectIndex("title1","supplierContactId")
							// })
							// _that.$refs.autoform.changeEditor("title1", "supplierContactId", {
							// 	"type": "editlinkbox",
							// 	"prompt": "请选择",
							// 	"linkParam":{
							// 		other: JSON.stringify({
							// 			tsupplierId: value[0].supplierId
							// 		}),
							// 		namefield:"userName",
							// 		initload:true,
							// 		url:"tsupplier/findSupplierContactList",
							// 		cachename:_that.supplierContactIdCacheKey
							// 	}
							// })
							for(let item of value){
								let data = Object.assign({},obj)
								for(let subItem of item.subData){
									if(subItem.checked){
										_that.$set(subItem,'batchNumber',subItem.newBatchNumber)
										_that.$set(subItem,'remark',subItem.rowRemark)  
										for(let dataIndex in fieldArr){
											_that.$set(data,dataIndex,subItem[dataIndex]?subItem[dataIndex]:'')
										}
										arr.push(subItem)
									}
								}
							}
							_that.om.request(
								'purchaseArrive/getListByIds',
								{
									dataStr:JSON.stringify(arr)
								},
								'post'
							).then(res=>{
								for(let index in res){
									delete arr[index].id
									delete arr[index].qualifiedInspectionNumber
									arr[index].unitId = res[index].unitId
									arr[index].unitName = res[index].unitName
									arr[index].batchNumber = res[index].batchNumber
									arr[index].actualArrivalQuantity = 	arr[index].actualArrivalQuantity
									arr[index].whetherGiftId = arr[index].isGiftId
									arr[index].whetherGiftName = arr[index].isGiftStr
									arr[index].batchUnqualifiedId = arr[index].batchUnqualifiedId
									arr[index].batchUnqualified = arr[index].batchUnqualified
									arr[index].batchInspectionConclusionId = arr[index].batchInspectionConclusionId
									arr[index].batchInspectionConclusion = arr[index].batchInspectionConclusion
									uni.setStorageSync('newProductIdKey', res[index].newProductId.toString())
									_that.om.request(
										'productShowSecond/getProductQualityControl',
										{
											productId:res[index].newProductId
										},
										'post'
									).then(zres=>{
										_that.om.request(
											'incomingInspectionReport/getProductUnqualifiedQuality',
											{
												supplierId: value[0].supplierId,
												newProductId: res[index].newProductId
											},'post').then(szres=>{
												arr[index].productUnqualifiedQuality = szres.value
												arr[index].testRulesId = zres.value.samplingRuleId
												arr[index].testRulesName = zres.value.samplingRuleName
												arr[index].inspectionLevelId = zres.value.inspectionLevelId
												arr[index].inspectionLevelName = zres.value.inspectionLevelName
												arr[index].inspectionSchemeId = zres.value.inspectionPlanId
												arr[index].inspectionSchemeName = zres.value.inspectionPlanName
												arr[index].inspectionMethodId = zres.value.samplingSchemeId
												arr[index].inspectionMethodName = zres.value.samplingSchemeName
												arr[index].valueAQLId = zres.value.valueAQLId
												arr[index].valueAQLName = zres.value.valueAQLName
												if(_that.subEditorObj["title3"]){
													_that.subEditorObj["title3"].workList = arr
												}
												
												_that.om.request(
													'incomingInspectionReport/getRecordPlanList',
													{
														actualArrivalQuantity: arr[index].actualArrivalQuantity,
														testRulesId: zres.value.samplingRuleId,
														inspectionLevelId: zres.value.inspectionLevelId,
														inspectionSchemeId: zres.value.inspectionPlanId,
														inspectionMethodId: zres.value.samplingSchemeId,
														valueAQLId: zres.value.valueAQLId
													}).then(tres=>{
														_that.om.request(
															'incomingInspectionReport/findInspectionRecordDataList',
															{
																productId:res[index].newProductId,
																actualSample: tres.value[0].actualSample || ''
															},
															'post'
														).then(zres=>{
															if(_that.subEditorObj["title4"]){
																_that.subEditorObj["title4"].workList = zres.value
															}
														})
													if(_that.subEditorObj["title5"]){
														let datalist = tres.value
														if(tres.value.length>0){
															_that.unqualifiedNumber = tres.value[0].unqualifiedNumber || -1
															uni.setStorageSync('unqualifiedNumberKey', _that.unqualifiedNumber.toString())
															_that.subEditorObj["title5"].workList = datalist
															_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataPlanStr",JSON.stringify(datalist))
															_that.$set(_that.formdata,"dataPlanStr",JSON.stringify(datalist))
														}else{
															uni.setStorageSync('unqualifiedNumberKey', '-1')
															_that.getDataPlan()
														}
													}
												})
										})
									})
								}
							})
							uni.removeStorageSync('chooseData')
						}
					}
				}catch(e){
					
				}
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
